//package com.zmn.plat.admin.controller.category.attribute;
//
//import java.util.Date;
//import java.util.List;
//import java.util.Objects;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import com.zmn.common.dto2.ResponseDTO;
//import org.apache.commons.lang.StringUtils;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
//import org.jetbrains.annotations.Nullable;
//import org.springframework.stereotype.Controller;
//import org.springframework.util.CollectionUtils;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.ResponseBody;
//
//import com.alibaba.fastjson.JSONObject;
//import com.zmn.common.constant.GlobalConsts;
//import com.zmn.common.constant.StatusConsts;
//import com.zmn.common.dto.ResultDTO;
//import com.zmn.common.utils.date.DateUtil;
//import com.zmn.common.utils.number.NumberUtil;
//import com.zmn.common.utils.string.StringUtil;
//import com.zmn.plat.admin.controller.category.CategoryBaseController;
//import com.zmn.plat.model.entity.category.BaseCategory;
//import com.zmn.plat.model.entity.category.BaseCategoryAttribute;
//
///**
// * @author duanzuocai
// * @version v1.0
// * @since 2018/10/20 9:15
// */
//@Controller
//@RequestMapping("/categ/attr/")
//public class CategoryAttributeModifyController extends CategoryBaseController {
//
//    @RequiresPermissions("/categ/attr/updateStatus.action")
//    @RequestMapping(value = "updateStatus")
//    @ResponseBody
//    public ResponseDTO updateStatus(HttpServletRequest request, HttpServletResponse response,
//                                    @RequestParam(value = "ids", defaultValue = "") String ids, @RequestParam(value = "status", defaultValue = "0") int status) {
//        if (StringUtil.isBlank(ids)) {
//            return ResponseDTO.fail("请选择要更改状态的属性！");
//        }
//
//        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
//        baseCategoryAttributeService.updateStatusBatch(arrayIds, status, getMcStaffRealName());
//
//        return ResponseDTO.success(true,"操作成功！");
//    }
//
//    // @RequiresPermissions("/categ/attr/updateSort.action")
//    @RequestMapping(value = "updateSort")
//    @ResponseBody
//    public ResponseDTO updateSort(HttpServletRequest request, HttpServletResponse response,
//        @RequestParam(value = "id", defaultValue = "0") Integer attrId, @RequestParam(value = "sort", defaultValue = "0") Integer sort) {
//        if (attrId == 0) {
//            return ResponseDTO.fail("请选择要更改排序的属性！");
//        }
//
//        if (sort == 0) {
//            return ResponseDTO.fail("请设置属性排序值！");
//        }
//
//        baseCategoryAttributeService.updateSortKey(attrId, sort, getMcStaffRealName());
//        return ResponseDTO.success(StatusConsts.STATUS_SUCCESS,"操作成功！");
//    }
//
//    @SuppressWarnings("Duplicates")
//    @RequestMapping(value = "addEdit")
//    @ResponseBody
//    public ResponseDTO addEdit(HttpServletRequest request, HttpServletResponse response, @RequestBody BaseCategoryAttribute attribute) {
//        Date now = DateUtil.getNow();
//        Integer attrId = attribute.getAttrId();
//
//        ResponseDTO x = checkAttribute(attribute);
//        if (x != null) {
//            return x;
//        }
//
//        try {
//            if (attrId == null) {
//                attribute.setCreater(getMcStaffRealName());
//                attribute.setCreateTime(now);
//                attribute.setUpdater(getMcStaffRealName());
//                attribute.setUpdateTime(now);
//                attribute.setSort(1);
//                attribute.setStatus(GlobalConsts.YES);
//                baseCategoryAttributeService.insert(attribute);
//            } else {
//                BaseCategoryAttribute baseCategoryAttribute = baseCategoryAttributeService.findByKey(attrId);
//                baseCategoryAttribute.setName(attribute.getName());
//                baseCategoryAttribute.setUpdater(getMcStaffRealName());
//                baseCategoryAttribute.setUpdateTime(now);
//                baseCategoryAttributeService.updateByKey(baseCategoryAttribute);
//            }
//
//            return ResponseDTO.success(StatusConsts.STATUS_SUCCESS,GlobalConsts.SUCCESS_MSG);
//        } catch (org.springframework.dao.DuplicateKeyException ex) {
//            logger.error("属性{}-{}已经存在，不能重复添加！", attribute.getCategId(), attribute.getName());
//            return ResponseDTO.fail("属性已经存在，不能重复添加！");
//        } catch (Exception ex) {
//            logger.error(String.format("[zmn]新增/修改产品分类属性失败,category:[%s],ex:[%s]", JSONObject.toJSONString(attribute), ex.getMessage()));
//            return ResponseDTO.fail("操作失败！");
//        }
//    }
//
//    @Nullable
//    private ResponseDTO checkAttribute(@RequestBody BaseCategoryAttribute attribute) {
//        Integer attrId = attribute.getAttrId();
//        // 渠道和通用产品属性分开处理
//        boolean isChannleAttr = Objects.equals(attribute.getAttrType(), GlobalConsts.YES);
//        if (StringUtils.isEmpty(attribute.getName())) {
//            return ResponseDTO.fail("属性名不能为空");
//        }
//
//        BaseCategory categ = baseCategoryService.findByKey(attribute.getCategId());
//
//        if (categ == null) {
//            return ResponseDTO.fail("分类不存在");
//        }
//        // 新增了渠道产品属性，渠道属性和通用属性不互相影响，存在渠道属性和通用属性相同的情况，因此需要去掉原来的唯一索引限制
//        List<BaseCategoryAttribute> categoryAttributes = baseCategoryAttributeService.listAttributeByCategIdAndAttrName(categ.getCategId(),
//            attribute.getChannelId(), attribute.getName());
//        if (!CollectionUtils.isEmpty(categoryAttributes)) {
//            if (NumberUtil.isNullOrZero(attrId) || categoryAttributes.size() > 1) {
//                return ResponseDTO.fail("属性已经存在，不能重复添加");
//            } else {
//                BaseCategoryAttribute attributeOne = categoryAttributes.get(0);
//                if (!Objects.equals(attrId, attributeOne.getAttrId())) {
//                    return ResponseDTO.fail("属性已经存在，不能重复添加");
//                }
//            }
//        }
//        // 通用产品的其他限制
//        if (!isChannleAttr) {
//            if (categ.getParentId() == GlobalConsts.TOP_ID) {
//                List<BaseCategoryAttribute> categoryAttributesSon =
//                    baseCategoryAttributeService.listAttributeFromChildByCategIdAndAttrName(categ.getCategId(), null, attribute.getName());
//                if (!CollectionUtils.isEmpty(categoryAttributesSon)) {
//                    if (NumberUtil.isNullOrZero(attrId) || categoryAttributesSon.size() > 1) {
//                        return ResponseDTO.fail("属性已经在其它子类中存在，不能重复添加");
//                    } else {
//                        BaseCategoryAttribute attributeOne = categoryAttributesSon.get(0);
//                        if (!Objects.equals(attrId, attributeOne.getAttrId())) {
//                            return ResponseDTO.fail("属性已经在其它子类中存在，不能重复添加");
//                        }
//                    }
//                }
//            } else {
//                List<BaseCategoryAttribute> categoryAttributesTop =
//                    baseCategoryAttributeService.listAttributeByCategIdAndAttrName(categ.getParentId(), null, attribute.getName());
//                if (!CollectionUtils.isEmpty(categoryAttributesTop)) {
//                    if (NumberUtil.isNullOrZero(attrId) || categoryAttributesTop.size() > 1) {
//                        return ResponseDTO.fail("属性已经在父类中存在，不能重复添加");
//                    } else {
//                        BaseCategoryAttribute attributeOne = categoryAttributesTop.get(0);
//                        if (!Objects.equals(attrId, attributeOne.getAttrId())) {
//                            return ResponseDTO.fail("属性已经在父类中存在，不能重复添加");
//                        }
//                    }
//                }
//            }
//        }
//        return null;
//    }
//}
